﻿/*
    This file is part of News Worm.

    News Worm is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    News Worm is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with News Worm.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Windows.Forms;

namespace NewsWorm {
    internal class RSS2Creator {
        
        internal RSS2Creator( Feed feed, string link ) {
            m_TempFile = Path.GetTempFileName();
            XmlTextWriter writer = new XmlTextWriter( m_TempFile, Encoding.Default );
            WriteRSSPrologue( writer, feed.Title, link);

            if ( !feed.IsCustomSearchFeed ) {
                foreach ( FeedItem item in feed.Items ) {
                    AddRSSItem( writer, item.ArticleTitle, item.ArticleUrl, item.ArticleSummary, item.ArticlePublished.ToString( "yyyy-MM-dd HH:mm" ) ); 
                }
            } else if ( feed.IsCustomSearchFeed ) {
                foreach ( FeedItem item in feed.CombinedSearchFeed.Items ) {
                    AddRSSItem( writer, item.ArticleTitle, item.ArticleUrl, item.ArticleSummary,item.ArticlePublished.ToString("yyyy-MM-dd HH:mm") ); 
                }
            }

            WriteRSSClosing( writer );
            writer.Close();
        }
        
        private string m_TempFile;

        private XmlTextWriter WriteRSSPrologue( XmlTextWriter writer, string FeedTitle, string link ) {
            writer.WriteStartDocument();
            writer.WriteStartElement( "rss" );
            writer.WriteAttributeString( "version", "2.0" );
            writer.WriteStartElement( "channel" );
            writer.WriteElementString( "title", FeedTitle );
            writer.WriteElementString( "link", link );
            writer.WriteElementString( "description",
               "Hosted RSS document generated by News Worm" );
            writer.WriteElementString( "copyright", "Copyright (c) Niko Rosvall <niko@newsworm.net>" );
            writer.WriteElementString( "generator", "News Worm v" + Application.ProductVersion );

            return writer;
        }

        private XmlTextWriter AddRSSItem( XmlTextWriter Writer,
                                                                 string ItemTitle, string ItemLink,
                                                                string ItemDescription, string date ) {
            Writer.WriteStartElement( "item" );
            Writer.WriteElementString( "title", ItemTitle );
            Writer.WriteElementString( "link", ItemLink );
            Writer.WriteElementString( "description", ItemDescription );
            Writer.WriteElementString( "pubDate", date );
            Writer.WriteEndElement();

            return Writer;
        }

        private XmlTextWriter WriteRSSClosing( XmlTextWriter writer ) {
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();

            return writer;
        }

        public override string ToString() {
            string str = File.ReadAllText( m_TempFile );
            File.Delete( m_TempFile );
            return str; 
        }

    }
}
